<template>
  <div class="entrance" v-if="!isFresh">
   
    <hunk-head name="马上安家" icon="none"></hunk-head>
    <!-- <div class="head">头部</div> -->
    <!-- userInfo.usertype -->
    <div class="wrapper" v-if="userInfo.usertype">
      <ManageHome v-if="path == 'managehome'"></ManageHome>
      <ManageHouseList style="height:100%" class="flex1" v-else-if="path == 'managehouseList'"></ManageHouseList>
      <ManageOrder v-else-if="path == 'manageorder'"></ManageOrder>
      <ManageAgency style="height:100%" v-else-if="path == 'manageagency'"></ManageAgency>
      <My v-else-if="path == 'my'"></My>
    </div>
    <div class="wrapper" v-else>
      <TenantHome v-if="path == 'tenanthome'"></TenantHome>
      <Tenantroomy v-else-if="path == 'tenantroomy'"></Tenantroomy>
      <Tenantlease style="height:100%" class="flex1" v-else-if="path == 'tenantlease'"></Tenantlease>
      <Tenantreserve style="height:100%" class="flex1" v-else-if="path == 'tenantreserve'"></Tenantreserve>
      <My v-else-if="path == 'my'"></My>
    </div>
    <hunk-tabbar v-if='islogin' @tabBarItem="tabBarItem" :tabBarList="userInfo.usertype && isbaojie == false?manageTabBarlist:!userInfo.usertype && isbaojie == false?tenantTabBarlist:baojieList"></hunk-tabbar>
 

  </div>
</template>

<script>
const imgUrl = `${process.env.VUE_APP_BASE_IMG_URL}`
import Head from "@componentsMine/head";
import Home from "../manage/home";
import HouseList from "../manage/houseList";
import Order from "../manage/order";
import Agency from "../manage/agency";
import My from "../my";

import TenantHome from "../tenant/home";
import Tenantroomy from "../tenant/roomy";
import Tenantlease from "../tenant/lease";
import Tenantreserve from "../tenant/reserve";
// import My from "../tenant/my";
import { GetUnreadCount } from '@/apis/user'

import TabBar from "@componentsMine/tabBar";

export default {
  components: {
    "hunk-head": Head,
    "ManageHome": Home,
    "ManageHouseList": HouseList,
    My,
    "ManageOrder": Order,
    "ManageAgency": Agency,
    TenantHome, Tenantroomy, Tenantlease, Tenantreserve,
    "hunk-tabbar": TabBar,
  },
   
  data() {
    return {
      isbaojie:false,
      islogin:false,
      interstitialAd:null,
      isFresh:false,
      showOpenSettingDialog: false, // 授权询问弹框，uview
      path: "",
      manageTabBarlist: [
        {
          path: 'managehome',
          name: '首页',
          iconPath: imgUrl+'tabBarObj/index.png',
          selectedIconPath: imgUrl+'tabBarObj/index_act.png'
        },
        {
          path: 'managehouseList',
          name: '房源',
          iconPath: imgUrl+'tabBarObj/house.png',
          selectedIconPath: imgUrl+'tabBarObj/house_act.png'
        },
        {
          path: 'manageorder',
          name: '订单',
          iconPath: imgUrl+'tabBarObj/order.png',
          selectedIconPath: imgUrl+'tabBarObj/order_act.png'
        },
        {
          path: 'manageagency',
          name: '待办',
          iconPath: imgUrl+'tabBarObj/agency.png',
          selectedIconPath: imgUrl+'tabBarObj/agency_act.png'
        },
        {
          path: 'my',
          name: '我的',
          iconPath: imgUrl+'tabBarObj/my.png',
          selectedIconPath: imgUrl+'tabBarObj/my_act.png'
        }
      ],
      tenantTabBarlist : [
        {
          path: 'tenanthome',
          name: '首页',
          iconPath: imgUrl+'tabBarObj/index.png',
          selectedIconPath: imgUrl+'tabBarObj/index_act.png'
        },
        {
          path: 'tenantroomy',
          name: '室友',
          iconPath: imgUrl+'tabBarObj/roomy.png',
          selectedIconPath: imgUrl+'tabBarObj/roomy_act.png'
        },
        {
          path: 'tenantlease',
          name: '租约',
          iconPath: imgUrl+'tabBarObj/lease.png',
          selectedIconPath: imgUrl+'tabBarObj/lease_act.png'
        },
        {
          path: 'tenantreserve',
          name: '预约',
          iconPath: imgUrl+'tabBarObj/reserve.png',
          selectedIconPath: imgUrl+'tabBarObj/reserve_act.png'
        },
        {
          path: 'my',
          name: '我的',
          iconPath: imgUrl+'tabBarObj/my.png',
          selectedIconPath: imgUrl+'tabBarObj/my_act.png'
        }
      ],
      baojieList:[
          {
          path: 'managehome',
          name: '首页',
          iconPath: imgUrl+'tabBarObj/index.png',
          selectedIconPath: imgUrl+'tabBarObj/index_act.png'
        },
         {
          path: 'my',
          name: '我的',
          iconPath: imgUrl+'tabBarObj/my.png',
          selectedIconPath: imgUrl+'tabBarObj/my_act.png'
        }
      ],
      noLoginList:[]
    };
  },
  computed: {
    // 获取tabBar的数据
    userInfo(){
      return this.$store.state.baseStore.userInfo
    },
  },
  created() {
    console.log(this.userInfo.usertype)
    if(this.userInfo.usertype ==1 && this.userInfo.roleid == 204){
      console.log('131313131313324')
      this.isbaojie =true
    }  else{
      this.isbaojie = false
    }
    if(this.userInfo.usertype ==1 && this.userInfo.enterpriseid == null){
      this.islogin =false
    }  else{
       this.islogin =true
    }
   
    this.path =  this.userInfo.usertype ? "managehome" : "tenanthome"
    // this.path = "managehome"
    console.log('9999999userinfo',this.$store.state.baseStore.userInfo)
    this.sGetUnreadCount()
  },
  onPullDownRefresh(){
    this.isFresh = true
    setTimeout(()=>{
      this.isFresh = false
    },200)
    uni.stopPullDownRefresh()  
  },
  onLoad() {
    switch(uni.getSystemInfoSync().platform){
      case 'android':
        console.log('运行Android上')
        break;
      case 'ios':
        console.log('运行iOS上')
        break;
      case 'devtools':
        console.log('运行在小程序')
        break;
    }
  },
  methods: {
    judgementMessageSwitch(flag) {
				let that = this
				uni.getSetting({
					withSubscriptions: true,
					success(res) {
						console.log(res)
						if (!res.subscriptionsSetting.mainSwitch) { // 订阅消息的总开关,如果是关着的，引导用户去打开
							that.showOpenSettingDialog = true
						} else { // 如果开着，则继续向下打开弹窗，获取用户授权
							that.messageSubscription()
						}
 
					},
					fail() {
						that.messageSubscription() // 如果失败，则继续向下打开弹窗，获取用户授权
					}
				})
			},
 
			// 弹窗点订阅，开启消息订阅提醒
			openSetting() {
				if(this.buttonConfirmText == '确定'){
					this.messageSubscription()
					return
				}
				
				var that = this
				uni.openSetting({
					withSubscriptions: true,
					complete(res) {
						uni.getSetting({
							withSubscriptions: true,
							success(res) {
								if (res.subscriptionsSetting.mainSwitch) { // 订阅消息的总开关,如果是开着的
									that.tipsContent = '再次点击确定，弹出可订阅列表进行订阅'
									that.buttonCancelText = '取消'
									that.buttonConfirmText = '确定'
								} else {
									that.showOpenSettingDialog = false;
								}
							}
						})
					}
				})
			},
 
			// 弹窗点不订阅
			dontOpenSetting() {
				this.showOpenSettingDialog = false;
			},
 
			// 订阅申请弹出，只允许点击弹出
			messageSubscription() {
				this.tipsContent= '为了及时掌握订单状态，您是否想接收订单状态的消息提醒？' // 弹框提示内容，
				this.buttonConfirmText= '去开启消息提醒'
				this.buttonCancelText= '不需要提醒'
				let that = this
				that.showOpenSettingDialog = false;
				let tmplId = ['48y4VNZO7YvZw_NHgWLT6WSJrR9OoECrBwHPKrzM2Uc'] // 模板ID，去小程序后台管理开一个，写进数组里，官方文档也有介绍
				uni.requestSubscribeMessage({
					tmplIds: tmplId,
					success(res) {
						console.log("success", res)
						if (res['48y4VNZO7YvZw_NHgWLT6WSJrR9OoECrBwHPKrzM2Uc'] == 'accept') {
					// 		  uni.showToast({
          //   title: '订阅成功',
          //   icon: 'none'
          // })
						}
					},
				})
			},
    sGetUnreadCount(){
        GetUnreadCount({
          type:+this.userInfo.usertype===0?'0':'1',
          userid:this.userInfo.userid
        }).then(e=>{
          if(e.data.unreadCount>0){
            if(+this.userInfo.usertype===0){
              // console.log(this.tenantTabBarlist[3])
              this.$set(this.tenantTabBarlist,3, {
                path: 'tenantreserve',
                name: '预约',
                babel:e.data.unreadCount,
                iconPath: imgUrl+'tabBarObj/reserve.png',
                selectedIconPath: imgUrl+'tabBarObj/reserve_act.png'
              })
            }else{
              this.$set(this.manageTabBarlist,3, {
                babel:e.data.unreadCount,
                path: 'manageagency',
                name: '待办',
                iconPath: imgUrl+'tabBarObj/agency.png',
                selectedIconPath: imgUrl+'tabBarObj/agency_act.png'
              })
            }
          }
        })
    },
    tabBarHead(res) {
      this.tabBarAllHeight = res
    },
    tabBarItem(item) {  
      this.name = item.name;
      this.path = item.path;
      let that = this
      console.log(this.userInfo.enterpriseid)
      if(this.path == 'manageagency'){
          that.judgementMessageSwitch()
      }
      if(this.path == 'tenantreserve'){
          console.log('231313')
          that.judgementMessageSwitch()
      }
       
    },
  },
};
</script>

<style lang="scss" scoped>
.adContainer{
  width: 100%;
}
.entrance {
  @include num();
  // padding-bottom为了减去底部tabbar的高度
  // padding-bottom: 50px;
  display: flex;
  flex-direction: column;
  background: #F9F9F9;
  .wrapper {
    // padding-bottom: 50px;
    flex: 1;
    // box-sizing: border-box;
    // overflow-x: hidden;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
  }
  .flex1{
    flex: 1;
    display: flex;
  }
}
</style>
